<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;
use Card\Controller\PublicController;
use Card\Model\CardMemberModel;
use Card\Api\WwsApi;


/**
 * 云名片用户
 */
class UserController extends PublicController {

    /* 是否登录 */
    public function islogin(){
        // $data = inputJson();
        $data = inputJson();
        // $data['phpsid'] = 'ofvbs70duforigpk5jcookors0';

        $res = $this->is_login($data['phpsid']);

        if($res){
            successOutputJson();
        }else{
            abnormalOutputJson(['info'=>'请先登录']);
        }
    }
 
    /**
     * 云名片用户登录/注册
     */
    public function login(){
        
        $data = inputJson();
         // $data['mobile'] = '15217710480';
         // $data['verify'] = '490921';
         // $data['name'] = 'lsx';
         // $data['msm_verify'] = '783953';
         // $data['phpsid'] = 'xinopenid';
         // $data['openId'] = '123';//openId


        /* 检测验证码 TODO: */
        $cdata['phpsid'] = $data['phpsid'];
        $cdata['code'] = $data['verify'];
        if(!check_verify($cdata,1,1)){
           abnormalOutputJson(['info'=>'验证码输入错误！','type'=>1]);
        }

        /* 检测手机号 */
        if (!preg_match("/^1[34578]\d{9}$/", $data['mobile'])) {
            abnormalOutputJson(['info' => '手机格式错误','type'=>2]);
        }


        /* 检测短信验证码 */
        $sms_log_model = D('SmsLog');
        $res = $sms_log_model->getSmsInfo(array('log_phone' => $data['mobile']));
        if ($res === false) {
            errorOutputJson('k' . __LINE__);
        } elseif (empty($res)) {
            errorOutputJson('k' . __LINE__);
        } else {
             if (($res['log_captcha'] != $data['msm_verify']) || ($res['is_use'] == 1)) {
                   abnormalOutputJson(['info' => '短信验证码错误！','type'=>3]);
             }
             /* 检测验证码有没有过期 30分钟过期*/
             if ((time() - $res['add_time']) > 60 * 30) {
                   abnormalOutputJson(['info' => '验证码已过期，请重新获取！','type'=>3]);
             }
        }
        $WwsApi = new WwsApi();
        //判断是登录还是注册
        $CardMember = D('CardMember');
        $res = $CardMember->getMemberInfo(array('mobile' => $data['mobile']));
        if ($res === false) {
            errorOutputJson('k' . __LINE__);
        } elseif (empty($res)) {
            //没有手机号用户看有没有openId用户，有openId用户则把手机号加到openId用户，没有openId用户则完全是新用户，新增手机号和openId
            //判断openId是否为空，为空则新增数据，不需要查询
            if(empty($data['openId'])){
                if(empty($data['name'])){
                    abnormalOutputJson(['info'=>'姓名不能为空','type'=>4]);//姓名不为空
                }
                //完全新用户 注册
                $mem_data['mobile'] = $data['mobile'];
                $mem_data['wechat'] = $data['openId'];
                $mem_data['name'] = $data['name'];
                $mem_data['session_id'] = session_id();
                $session_id = $mem_data['session_id'];

                $CardMember->sx_data=$mem_data;
                $res = $CardMember->addMember();
                if(isset($res['msg'])){
                    abnormalOutputJson(['info'=>$res['msg'],'type'=>2]);
                }elseif(!$res){
                    errorOutputJson('k'.__LINE__);
                }
                $user_id = $res;//用户id
            }else{
                $res = $CardMember->getMemberInfo(array('wechat' => $data['openId']));
                if($res === false){
                    errorOutputJson('k' . __LINE__);
                }elseif (!empty($res)) {
                    if(empty($data['name'])){
                        abnormalOutputJson(['info'=>'姓名不能为空','type'=>4]);//姓名不为空
                    }
                    
                    //有openId,修改openId用户手机号
                    $user_id = $res['id'];//用户id
                    $session_id = $res['session_id'];
                    $mem_data['mobile'] = $data['mobile'];
                    $mem_data['name'] = $data['name'];
                    $res = $CardMember->where(array('id'=>$res['id']))->save($mem_data);
                    if($res === false){
                        errorOutputJson('k' . __LINE__);
                    }
                }else{
                    if(empty($data['name'])){
                        abnormalOutputJson(['info'=>'姓名不能为空','type'=>4]);//姓名不为空
                    }
                    //完全新用户 注册
                    $mem_data['mobile'] = $data['mobile'];
                    $mem_data['wechat'] = $data['openId'];
                    $mem_data['name'] = $data['name'];
                    $mem_data['session_id'] = session_id();
                    $session_id = $mem_data['session_id'];

                    $CardMember->sx_data=$mem_data;
                    $res = $CardMember->addMember();
                    if(isset($res['msg'])){
                        abnormalOutputJson(['info'=>$res['msg'],'type'=>2]);
                    }elseif(!$res){
                        errorOutputJson('k'.__LINE__);
                    }
                    $user_id = $res;//用户id
                }
            }
            

            //创建云名片
            $udata['name'] = $data['name'];
            $udata['moblie_phone'] = $data['mobile'];
            $User = D('User');
            $User->sx_data=$udata;
            $res = $User->addCart();
            if(isset($res['msg'])){
                abnormalOutputJson(['info'=>$res['msg'],'type'=>4]);//姓名不为空
            }elseif(!$res){
                errorOutputJson('k'.__LINE__);
            }else{
                $cart_id = $res;//云名片id
            }

            $res = $this->qrurl($cart_id);
            if($res === false){//没有生成名片
                errorOutputJson('k'.__LINE__);
            }
            $code_data['card_code'] = $res;
            $code_data['id'] = $cart_id;
            $res = D('User')->save($code_data);
            if($res === false){
                errorOutputJson('k'.__LINE__);
            }

            //存储用户id云名片id
            $CardId = D('CardId');
            $idata['user_id'] = $user_id;
            $idata['cart_id'] = $cart_id;
            $res = $CardId->getCidInfo(array('user_id'=>$user_id));
            if(empty($res)){
                $CardId->sx_data=$idata;
                $res = $CardId->addCardId();
                if(isset($res['msg'])){
                    errorOutputJson('k'.__LINE__);
                }elseif(!$res){
                    errorOutputJson('k'.__LINE__);
                }
            }else{
                $idata['id'] = $res['id'];
                $res = $CardId->save($idata);
                if($res === false){
                    errorOutputJson('k'.__LINE__);
                }
            }

            

            //修改短信验证码状态
            $sms['log_phone'] = $data['mobile'];
            $sms['is_use'] = 1;
            $SmsLog = D('SmsLog');
            $res = $SmsLog->saveSms($sms);

            
            //通知接口，是否存在该手机号,存在则
            $member_data = array(
                'phone'     =>  $data['mobile']
            );
            $exist_member = $WwsApi->checkUserExist($member_data);
            if($exist_member['status'] == 1) {
                //更新用户表
                $update_data = array(
                    'member_uuid'       =>  $exist_member['data']
                );
                $member_result = $CardMember->where(['id'=>$user_id])->save($update_data);
            }
            

            successOutputJson($session_id,['info'=>'注册成功']);//返回session_id            
            
        
        } else {
            //登录

            //修改登录信息
            
            //如果手机号对应的openId和当前的openId不一致，则在覆盖旧的openId前，需要合并新openId的旧的贺卡数据。
            if(!empty($data['openId']) && ($res['wechat'] != $data['openId'])){
                //查新openId的用户id
                $CardMember = D('CardMember');
                $useda = $CardMember->where(['wechat'=>$data['openId']])->find();
                if(!empty($useda)){
                    $new_user_id = $useda['id'];
                    //根据用户id把所属贺卡的用户id改成手机号用户的
                    $old_user_id = $res['id'];//手机号用户id
                    $bcardData['user_id'] = $old_user_id;
                    $bcard_lists = D('GreetingCard')->where(['user_id'=>$new_user_id])->save($bcardData);
                    if($bcard_lists === false){
                        errorOutputJson('k' . __LINE__);
                    }

                    //合并登录次数
                    $new_login = intval($res['login']) + intval($useda['login']);
                    $login['login'] = $new_login;
                    $lores = $CardMember->where(['id'=>$old_user_id])->save($login);
                    if($lores === false){
                        errorOutputJson('k' . __LINE__);
                    }

                    //删除用户
                    $delres = $CardMember->where(['id'=>$new_user_id])->delete();
                    if($delres === false){
                        errorOutputJson('k' . __LINE__);
                    }
                }
            }
            $mem_data['id'] = $res['id'];
            if(!empty($data['openId'])){
                $mem_data['wechat'] = $data['openId'];
            }
            $CardMember->sx_data = $mem_data;
            $res = $CardMember->saveMember();
            if ($res === false) {
                errorOutputJson('k' . __LINE__);
            } else {
               //查session_id
               $condition = array('id' => $mem_data['id']);
               $res = D('CardMember')->getMemberInfo($condition);
               $member_uuid = $res['member_uuid'];
               if (!$res) {
                    errorOutputJson('k' . __LINE__);
               } else {
                    $session_id = $res['session_id'];

                    //如果注册时因错误未生成名片，则登录时创建
                    $user_id = $res['id'];//用户id
                    //查询是否有名片
                    $CardId = D('CardId');
                    $rres = $CardId->getCidInfo(array('user_id'=>$user_id));
                    
                    if($rres === false){
                        errorOutputJson('k'.__LINE__);
                    }elseif (empty($rres) || empty($rres['cart_id']) || $rres['cart_id'] == 0) {
                        //没有名片,创建云名片
                        $udata['name'] = $res['name'];
                        $udata['moblie_phone'] = $res['mobile'];
                        $User = D('User');
                        $User->sx_data=$udata;
                        $res = $User->addCart();
                        if(!$res){
                            errorOutputJson('k'.__LINE__);
                        }

                        $cart_id = $res;//云名片id
                        $res = $this->qrurl($cart_id);
                        if($res === false){//没有生成名片
                            errorOutputJson('k'.__LINE__);
                        }
                        $code_data['card_code'] = $res;
                        $code_data['id'] = $cart_id;
                        $res = D('User')->save($code_data);
                        if($res === false){
                            errorOutputJson('k'.__LINE__);
                        }

                        //存储用户id云名片id
                        $idata['user_id'] = $user_id;
                        $idata['cart_id'] = $cart_id;
                        //注册没有生成名片
                        if (empty($rres)){
                            $CardId->sx_data=$idata;
                            $res = $CardId->addCardId();
                        }elseif(empty($rres['cart_id'])){//注册没有生成名片id
                            $res = $CardId->saveCart(array('user_id'=>$user_id),$idata);
                        }
                        if(isset($res['msg'])){
                            errorOutputJson('k'.__LINE__);
                        }elseif($res === false){
                            errorOutputJson('k'.__LINE__);
                        }
                    }

                    //修改验证码状态
                    $sms['log_phone'] = $data['mobile'];
                    $sms['is_use'] = 1;
                    $SmsLog = D('SmsLog');
                    $res = $SmsLog->saveSms($sms);

                    //如果还未与平台账号统一
                    if($member_uuid == 0) {
                        //通知接口，是否存在该手机号,存在则
                        $member_data = array(
                            'phone'     =>  $data['mobile']
                        );
                        $exist_member = $WwsApi->checkUserExist($member_data);
                        if($exist_member['status'] == 1) {
                            //更新用户表
                            $update_data = array(
                                'member_uuid'       =>  $exist_member['data']
                            );
                            $member_result = $CardMember->where(['id'=>$mem_data['id']])->save($update_data);
                        }
                    }

                    

                    successOutputJson($session_id,['info'=>'登录成功']);//ofvbs70duforigpk5jcookors0
               }
            }
        }

    }
    /**
     * 游客登录(去制作贺卡点击暂不注册)
     */
    public function TouristLogin(){
        $data = inputJson();

        // $data['openId'] = '456';//openId

        if(empty($data['openId'])){
            abnormalOutputJson(['info'=>'参数错误']);
        }

        //判断是登录还是注册
        $CardMember = D('CardMember');
        $res = $CardMember->getMemberInfo(array('wechat' => $data['openId']));
        if ($res === false) {
            errorOutputJson('k' . __LINE__);
        } elseif (empty($res)) {
            //注册
            $mem_data['wechat'] = $data['openId'];
            $mem_data['session_id'] = session_id();
            $CardMember->sx_data=$mem_data;
            $res = $CardMember->addTouristMember();
            if(!$res){
                errorOutputJson('k'.__LINE__);
            }else{
                successOutputJson($mem_data['session_id']);//返回session_id
            }
        } else {
            //登录
            
            //修改登录信息
            $session_id = $res['session_id'];
            $mem_data['id'] = $res['id'];
            $CardMember->sx_data = $mem_data;
            $res = $CardMember->saveMember();
            if($res === false){
                errorOutputJson('k'.__LINE__);
            }else{
                successOutputJson($session_id);//ofvbs70duforigpk5jcookors0
            }
        }

    }
     /**
     * 判断是否是游客
     */
    public function is_Tourist(){
        $data = inputJson();

        // $data['phpsid'] = 'rnk59damsne1qomicmuai31d62';

        if(empty($data['phpsid'])){
            //没有登录
            $is_Tourist = 3;
            successOutputJson($is_Tourist);
        }
        $CardMember = D('CardMember');
        $res = $CardMember->getMemberInfo(array('session_id' => $data['phpsid']));
        if($res === false){
            errorOutputJson('k'.__LINE__);
        }elseif(empty($res)){
            //没有登录
            $is_Tourist = 3;
        }elseif(empty($res['mobile'])){
            //游客
            $is_Tourist = 2;
        }else{
            //正式用户
            $is_Tourist = 1;
        }

        successOutputJson($is_Tourist);

    }
    /**
     * 新增用openId判断有没有绑定手机号，有则不能去到登录页(防止绑定不同的手机号),合并判断是不是游客，游客即时有session_id，没有手机号，也要去手机号登录
     */
    public function useOpenIdisLogin(){

        $data = inputJson();

        // $data['openId'] = 'omvyF1YRBcGdDPy4DK0t50KJVmao';
        // $data['phpsid'] = '';
        // $data['makeCard'] = 0;//1-名片

        //两个都空则没有登录
        if(empty($data['openId']) && empty($data['phpsid'])){
            $is_login = -1;
            successOutputJson($is_login);
        }
        if(!empty($data['phpsid'])){
            //如果数据库存在session_id,则已经登录了，但是在选择制作名片按钮要判断有没有绑定手机号，没有仍要登录；
            $res = D('CardMember')->where(['session_id'=>$data['phpsid']])->find();
            if($res === false){
                errorOutputJson('k'.__LINE__);
            }elseif (!empty($res)) {
                if($data['makeCard'] === 1){
                    if(empty($res['mobile'])){
                        $is_login = -1;
                    }else{
                        $is_login = 1;
                    }
                }else{
                    $is_login = 1;
                }
            }else{
                $is_login = -1;
            }
            
        }else{
            //session_id为空，但openId不为空
            $res = D('CardMember')->where(['wechat'=>$data['openId']])->find();
            if($res === false){
                errorOutputJson('k'.__LINE__);
            }elseif (!empty($res)) {
                //判断有没有绑定手机号，有则跳过登录，但是需要更改登录信息，没有则去到登录页
                if(!empty($res['mobile'])){
                    $is_login = 1;

                    //修改登录信息
                    $CardMember = D('CardMember');
                    $udata['id'] = $res['id'];
                    $CardMember->sx_data = $udata;
                    $res = $CardMember->saveMember();
                    if ($res === false) {
                        errorOutputJson('k' . __LINE__);
                    }
                }elseif(empty($res['mobile']) && $data['makeCard'] == 0){
                    $is_login = 1;

                    //修改登录信息
                    $CardMember = D('CardMember');
                    $udata['id'] = $res['id'];
                    $CardMember->sx_data = $udata;
                    $res = $CardMember->saveMember();
                    if ($res === false) {
                        errorOutputJson('k' . __LINE__);
                    }
                }else{
                    $is_login = -1;
                }
            }else{
                //没有该openId
                $is_login = -1;
            }
        }
        successOutputJson($is_login);
    }
    /**
     * 是否绑定手机号
     */
    public function is_mobile(){
        $data = inputJson();

        // $data['openId'] = '123';
        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        
        $is_mobile = 0;
        $is_Tourist = 3;

        if(empty($data['openId']) && empty($data['phpsid'])){
            $is_mobile = 0;
            $is_Tourist = 3;
        }
        $session_id = $data['phpsid'];

        if(!empty($data['phpsid'])){
            $res = D('CardMember')->where(['session_id'=>$data['phpsid']])->find();
            $session_id = $res['session_id'];
            if($res === false){
                errorOutputJson('k'.__LINE__);
            }elseif(empty($res)){
                $is_mobile = 0;
                $is_Tourist = 3;
            }elseif(!empty($res['mobile'])){
                $is_mobile = 1;
                $is_Tourist = 1;
            }else{
                $is_mobile = 0;
                $is_Tourist = 2;
            }
        }elseif(!empty($data['openId'])){
            $res = D('CardMember')->where(['wechat'=>$data['openId']])->find();
            $session_id = $res['session_id'];
            if($res === false){
                errorOutputJson('k'.__LINE__);
            }elseif(empty($res)){
                $is_mobile = 0;
                $is_Tourist = 3;
            }elseif(!empty($res['mobile'])){
                $is_mobile = 1;
                $is_Tourist = 1;

                //修改登录信息
                $CardMember = D('CardMember');
                $udata['id'] = $res['id'];
                $CardMember->sx_data = $udata;
                $res = $CardMember->saveMember();
                if ($res === false) {
                    errorOutputJson('k' . __LINE__);
                }
            }else{
                $is_mobile = 0;
                $is_Tourist = 2;
            }
        }

        successOutputJson(['phpsid'=>$session_id,'openId'=>$data['openId'],'is_mobile'=>$is_mobile,'is_Tourist'=>$is_Tourist]);
    }


    /* 文字验证码初始化 */
    public function verifyInit(){
        $data['session_id'] = session_id();
        $VerifLog = D('VerifLog');
        $VerifLog->sx_data=$data;
        $res = $VerifLog->addVerif();
        if($res === false){
            errorOutputJson('k'.__LINE__);
        }else{
            successOutputJson($data['session_id']);
        }
    }
    /* 文字验证码 */
    public function verify(){
        $verify = new \Think\Verify();
        $verify->fontSize = 50;
        $verify->length = 4;
        $verify->imageW = 400;
        $verify->imageH = 110;
        $verify->codeSet = '0123456789';
        $verify->useCurve = false;
        $verify->sxentry(1);
    }
    /* 验证文字验证码 */
    public function check_verify(){
        $data = inputJson();
        // $data['phpsid'] = 'gb3ad25lu6gn1o9jp33ao9tm76';
        // $data['code'] = '1234';

        if(!check_verify($data)){
            abnormalOutputJson(['info'=>'验证码错误']);
        }else{
            successOutputJson();
        }
    }

    /**
    * 发送手机验证码
    * @param string $phone 手机号
    * @param string $log_type 短信类型:1为注册,2为登录
    * @return array
    */
    public function sendCaptcha(){
        // $data = inputJson();
        $data = inputJson();

        //假数据
        // $data['phone'] = '18826075508';

        $phone = $data['phone'];
        //查询是注册还是登陆(区分验证码内容)
        $model_member = D('CardMember');
        $is_member = $model_member->getMemberInfo(array('mobile' => $phone));
        if (!empty($is_member)) {
          //登录
          $log_type = 2;
        } else {
          //注册
          $log_type = 1;
        }

        $model_sms_log = D('SmsLog');
        $state = true;
        $msg = '手机验证码发送成功';

        //查询是否存在同一手机号同一类型记录,60秒内不能发送多次
        $condition = array();
        $condition['log_phone'] = $phone;
        $condition['log_type'] = $log_type;
        $sms_log = $model_sms_log->getSmsInfo($condition);
        if (!empty($sms_log) && ($sms_log['add_time'] > (time() - C('SEND_SMSCODE')['DEFAULT_CONNECT_SMS_TIME']))) {//同一手机号IP[n]秒内只能发一条短信
          $state = false;
          $msg = '同一手机号' . C('SEND_SMSCODE')['DEFAULT_CONNECT_SMS_TIME'] . '秒内，请勿多次获取验证码！';
        }

        if ($state == true) {
            $log_array = array();
            $model_member = D('CardMember');
            $member = $model_member->getMemberInfo(array('mobile' => $phone));

            $captcha = rand(100000, 999999);

            $log_msg = '您于' . date("Y-m-d");
            //            $log_msg = '【'.C('site_name').'】您于'.date("Y-m-d");
            switch ($log_type) {
                case '1':
                  if (!empty($member)) {//检查手机号是否已被注册
                      $state = false;
                      $msg = '当前手机号已被注册，请更换其他号码。';
                  }else{
                    $log_msg .= '申请注册会员，验证码：' . $captcha . '。';
                  }
                  break;
                case '2':
                  if (empty($member)) {//检查手机号是否已绑定会员
                    $state = false;
                    $msg = '当前手机号未注册，请检查号码是否正确。';
                  }else{
                    $log_msg .= '申请登录，验证码：' . $captcha . '。';
                  }
                  break;
                default:
                  $state = false;
                  $msg = '参数错误';
                break;
            }
            if ($state == true) {
                $result = $this->sms_verify($phone, $log_msg);
                if ($result == 0) {
                  $log_array['log_phone'] = $phone;
                  $log_array['log_captcha'] = $captcha;
                  $log_array['log_ip'] = getIp();
                  $log_array['log_msg'] = $log_msg;
                  $log_array['log_type'] = $log_type;
                  $log_array['add_time'] = time();
                  $model_sms_log->addSms($log_array);
                  successOutputJson($log_type);

                } else {
                  abnormalOutputJson(['info'=>'手机短信发送失败']);
                }
            }else{
                abnormalOutputJson(['info'=>$msg]);
            }
        }else{
          abnormalOutputJson(['info'=>$msg]);
        }
    }

    /* 照片贺卡多少张 */
    public function cardTotal(){
        $data = inputJson();
        // $data['phpsid'] = '13hga3tn6o2v82smdmm2a0r9a4';

        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        //用户id
        $user_id = $this->get_userId($data['phpsid']);
        if($user_id === false){
            errorOutputJson('e'.__LINE__);
        }elseif ($res === []) {
            abnormalOutputJson(['info'=>'请先登录']);//传了空session_id或查不到
        }

        //查名片id
        $card_id = $this->getCardId($data['phpsid']);//名片id
        if($card_id === -1){
            abnormalOutputJson(['info'=>'请先登录']);
        }elseif ($card_id === false) {
            errorOutputJson('e'.__LINE__);
        }elseif(empty($card_id)){
            $card_id = -1;
        }

        //查图片id，计算多少张
        $res = D('CardId')->getCidInfo(array('user_id'=>$user_id),'img_id');
        if(empty($res['img_id'])){
            $img_total = 0;
        }else{
            $img_id = explode(',', $res['img_id']);
            $img_total = 0;
            foreach ($img_id as $k => $v) {
                $img_total ++;
            }
        }
        // var_dump($img_total);exit;//照片总数
        //贺卡总数
        $card_total = D('GreetingCard')->where(array('user_id'=>$user_id,'delete_state'=>0))->count();

        successOutputJson(['imgs_total'=>$img_total,'bcard_total'=>$card_total,'card_id'=>$card_id]);
        
    }

    /* 记录openId和名片id */
    public function OpenIdRecord(){
        $data = inputJson();
        // $data['openId'] = '123';
        // $data['card_id'] = 7;

        if(empty($data['card_id']) || empty($data['openId'])){
            abnormalOutputJson(['info'=>'参数错误']);
        }

        //判断是否本人查看，是的话不记录
        $res = D('CardMember')->where(['wechat'=>$data['openId']])->find();
        $user_id = $res['id'];
        $res = D('CardId')->where(['cart_id'=>$data['card_id']])->find();
        $card_user_id = $res['user_id'];
        if($user_id != $card_user_id){
            //查询用户之前是否查看过改名片，有则增加次数，没有则新增记录
            $res = D('OpenidCid')->where(['openId'=>$data['openId'],'card_id'=>$data['card_id']])->find();
            if(empty($res)){
                //增加记录
                $data['utime'] = time();
                $data['num'] = 1;
                $res = D('OpenidCid')->add($data);
                if(!$res){
                    errorOutputJson('k'.__LINE__);
                }
            }else{
                //增加查看次数
                $data['id'] = $res['id'];
                $data['utime'] = time();
                $res = D('OpenidCid')->update($data);
                if($res === false){
                    errorOutputJson('k'.__LINE__);
                }
            }
        }else{
            //如果数据库存在本人查看的数据，则把他们删除
            $res = D('OpenidCid')->where(['openId'=>$data['openId'],'card_id'=>$data['card_id']])->find();
            if(!empty($res)){
                $res = D('OpenidCid')->where(['openId'=>$data['openId'],'card_id'=>$data['card_id']])->delete();
            }
            
        }
        successOutputJson();

    }

    public function onWXOpenid()
    {
        $data = inputJson();
        
        $cardMemberModel = new CardMemberModel();
        $result = $cardMemberModel->existCardMember([
            'wechat' => $data['openid']
                ]);
        if ($result !== false) {
            if ($result === null) {
                $time = time();
                $sid = session_id();
                $cardMemberModel->addCardMember([
                    'last_login_time' => $time,
                    'login' => 1,
                    'wechat' => $data['openid'],
                    'reg_time' => $time,
                    'reg_ip' => get_client_ip(1),
                    'last_login_ip' => get_client_ip(1),
                    'session_id' => $sid
                ]);
                
            } else {
                $sid = $cardMemberModel->getSid($data['openid']);
            }
            successOutputJson2(['sid' => $sid]);
        } else
            errorOutputJson2('查询数据出错'.__LINE__);
    }

}
